package org.syncsoft.overalt.gui.util;

import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @pattern BUILD
 * 
 * @author tptfc
 *
 */
public class BuildFormWindow {
	private static final int PADDING = 10;
	private JComponent component;
	private int currentY;
	private boolean increment;
	
	{
		setIncrement(true);
	}
	
	public BuildFormWindow() {
		this(new JPanel());
	}
	
	public BuildFormWindow(JComponent component) {
		if (component != null) {
			setComponent(component);
			getComponent().setLayout(new GridBagLayout());
		} else {
			setComponent(new JPanel());
		}
	}
	
	/**
	 * 
	 * @param label
	 * @param text
	 */
	public void add(JLabel label, JTextField text) {
		if (label != null && text != null) {
			GridBagConstraints layoutConstraints = addLabel(label);
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			getComponent().add(text, layoutConstraints);
			
			addCurrentY();
		}
	}
	
	/**
	 * 
	 * @param label
	 * @param text
	 */
	public void add(JLabel label, JComboBox combo) {
		if (label != null && combo != null) {
			GridBagConstraints layoutConstraints = addLabel(label);
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			getComponent().add(combo, layoutConstraints);
			
			addCurrentY();
		}
	}
	
	/**
	 * 
	 * @param label
	 * @param text
	 */
	public void add(JLabel label, JCheckBox checkBox) {
		if (label != null && checkBox != null) {
			GridBagConstraints layoutConstraints = addLabel(label);
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			getComponent().add(checkBox, layoutConstraints);
			
			addCurrentY();
		}
	}
	
	/**
	 * 
	 * @param label
	 * @param text
	 */
	public void add(JCheckBox checkBox) {
		if (checkBox != null) {
			GridBagConstraints layoutConstraints = new GridBagConstraints();
			layoutConstraints.fill = GridBagConstraints.BOTH;
			layoutConstraints.weightx = 0.5;
			
			layoutConstraints.insets = new Insets(PADDING, PADDING, 0, PADDING);
			
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			getComponent().add(checkBox, layoutConstraints);
			
			addCurrentY(true);
			layoutConstraints.insets = new Insets(0, PADDING, 0, PADDING);
			
			addCurrentY();
		}
	}	
	
	
	/**
	 * 
	 * @param label
	 * @param text
	 */
	public void add(JLabel label, JTextArea textArea) {
		if (label != null && textArea != null) {
			GridBagConstraints layoutConstraints = addLabel(label);
			
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			layoutConstraints.weighty = 0.1;
			getComponent().add(new JScrollPane(textArea), layoutConstraints);
			
			addCurrentY();
		}
	}
	
	/**
	 * 
	 * @param label
	 * @param text
	 */
	public void add(JLabel label, JList list) {
		if (label != null && list != null) {
			GridBagConstraints layoutConstraints = addLabel(label);
			
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			layoutConstraints.weighty = 0.1;
			getComponent().add(new JScrollPane(list), layoutConstraints);
			
			addCurrentY();
		}
	}
	
	/**
	 * 
	 * @param label
	 * @param table
	 */
	public void add(JLabel label, JTable table) {
		if (label != null && table != null) {
			GridBagConstraints layoutConstraints = addLabel(label);
			
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			layoutConstraints.weighty = 0.1;
			getComponent().add(new JScrollPane(table), layoutConstraints);
			
			addCurrentY();
		}
	}
	
	/**
	 * 
	 * @param label
	 * @param table
	 */
	public void add(JComponent panel) {
		if (panel != null) {
			GridBagConstraints layoutConstraints = new GridBagConstraints();
			layoutConstraints.fill = GridBagConstraints.BOTH;
			layoutConstraints.weightx = 0.5;
			layoutConstraints.insets = new Insets(0, PADDING, 0, PADDING);
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			layoutConstraints.weighty = 0.1;
			
			getComponent().add(panel, layoutConstraints);
			
			addCurrentY();
		}
	}
	
	/**
	 * 
	 * @param buttons
	 */
	public void add(JButton ...buttons) {
		if (buttons != null) {
			GridBagConstraints layoutConstraints = new GridBagConstraints();
			layoutConstraints.fill = GridBagConstraints.HORIZONTAL;
			layoutConstraints.weightx = 0.1;
			
			layoutConstraints.insets = new Insets(PADDING, PADDING, PADDING, PADDING);
			layoutConstraints.gridy = getCurrentY();
			layoutConstraints.gridx = 0;

			JPanel panel = new JPanel();
			panel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
			panel.setLayout(new FlowLayout(FlowLayout.LEADING));
			getComponent().add(panel, layoutConstraints);
			
			
			for (int i = 0; i < buttons.length; i++) {
				panel.add(buttons[i]);
			}
			
			addCurrentY();
			layoutConstraints.insets = new Insets(0, PADDING, 0, PADDING);
		}
	}
	
	/**
	 * @precondition separator not is null
	 * @param separator
	 */
	public void add(JSeparator separator) {
		if (separator != null) {
			GridBagConstraints layoutConstraints = new GridBagConstraints();
			layoutConstraints.fill = GridBagConstraints.BOTH;
			layoutConstraints.weightx = 0.5;
			
			layoutConstraints.insets = new Insets(PADDING, PADDING, 0, PADDING);
			
			layoutConstraints.gridx = 0;
			layoutConstraints.gridy = getCurrentY();
			getComponent().add(separator, layoutConstraints);
			
			addCurrentY(true);
		}
	}
	
	public JComponent build() {
		return getComponent();
	}

	/**
	 * add label
	 * @param label
	 * @return
	 */
	private GridBagConstraints addLabel(JLabel label) {
		GridBagConstraints layoutConstraints = new GridBagConstraints();
		layoutConstraints.fill = GridBagConstraints.BOTH;
		layoutConstraints.weightx = 0.5;
		
		layoutConstraints.insets = new Insets(PADDING, PADDING, 0, PADDING);
		
		layoutConstraints.gridx = 0;
		layoutConstraints.gridy = getCurrentY();
		getComponent().add(label, layoutConstraints);
		
		addCurrentY(true);
		layoutConstraints.insets = new Insets(0, PADDING, 0, PADDING);
		
		return layoutConstraints;
	}
	
	/**
	 * added the currentY variable
	 */
	private void addCurrentY() {
		addCurrentY(isIncrement());
	}
	
	/**
	 * added the currentY variable if the increment is true
	 */
	private void addCurrentY(boolean increment) {
		if (increment) {
			setCurrentY(getCurrentY() + 1);
		}
	}
	
	//	GETs AND SETs
	private JComponent getComponent() {
		return component;
	}
	private void setComponent(JComponent component) {
		this.component = component;
	}
	private int getCurrentY() {
		return currentY;
	}
	private void setCurrentY(int currentY) {
		this.currentY = currentY;
	}
	protected boolean isIncrement() {
		return increment;
	}
	public void setIncrement(boolean increment) {
		this.increment = increment;
	}
}
